/**
 * zmn 花名册模块-产品组
 * @author tanghongjun
 * @since 2020-08-19 11:18:36
 */
let flag = true;
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;

    form.render(null, "zmn-form");

    form.verify({
        reimbursementVerify: function (value) {
            if (value <= 0 || value > 9999999) {
                return "金额不能小于等于0或者大于9999999";
            }
            const reg = new RegExp("^[0-9]+.?[0-9]{0,2}$");
            if (!reg.test(value)) {
                return "请输入正确的金额格式!#.##";
            }
        }
    });

    zmnLayUtil.layFormVerify(form);
    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        var data = obj.field;
        var url = "/product/productgroup/addEdit.action";

        if (data.groupId > 0) {
            flag = false;
        }
        if (data.perOrderWarningMin * 1 >= data.perOrderWarningMax * 1) {
            layui.layer.msg("预警值范围设置有问题，左边必须小于右边", {"icon": 2});
            return false;
        }
        // 元转分
        if (zmnUtil.isNotNull(data.reimbursementAmount)) {
            data.reimbursementAmount = data.reimbursementAmount * 100;
        }

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (result) {
                if (result.status == 200) {
                    parent.layui.layer.msg(result.message, {time: 1000}, () => {
                        parent.location.reload();
                    });
                } else {
                    layui.layer.msg(result.message,{"icon":2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
            }
        })
        return false;
    });

    //查询框联动
    form.on('radio(bizTypeFilter)', function (data) {
        let bizType = data.value;
        let option = "<option value=\"\">-选择-</option>";
        $("#bizLine").html(option);
        $.ajax({
            async: false,
            type: "GET", url: "/product/businessline/listVtByBizType.action?bizType="+bizType,
            dataType: "json",
            success:function(result) {
                if(result.status == 200) {
                    let items = result.data;
                    items.forEach(item => {
                        option += "<option value='"+item.value+"'>"+item.text+"</option>";
                    });
                    $("#bizLine").html(option);
                } else {
                    layui.layer.msg(result.message,{"icon":2});
                }
                form.render("select");
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
            }
        });
        return false;
    });

    //对外暴露的接口
    exports('plat_productgroup_detail', {});
});

// 取消
function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}